<?php
$conn = mysqli_connect("localhost", "root", "", "shikin");
//mysql_select_db("shikin", $conn);
//search code
//error_reporting(0);

// $_REQUEST['submit'] // 获取请求中的submit参数

	header('Content-Type:application/json; charset=utf-8');

	$name = $_POST['name'];

	if (empty($name)) {
		$make = '<h4>You must type a word to search!</h4>';
	} 
	else {
		$make = '<h4>No match found!</h4>';
		$sele = "SELECT * FROM data WHERE name LIKE '%$name%'";
		$result = mysqli_query($conn, $sele);

		$arr = array();

		if ($row = mysqli_num_rows($result) > 0) {
			while ($row = mysqli_fetch_assoc($result)) {
				// echo '<h4> Id						: ' . $row['id'];
				// echo '<br> name						: ' . $row['name'];
				// echo '</h4>';
				
				// $arr = array('a'=>1,'b'=>2);
				$obj = array('id'=>$row['id'],'name'=>$row['name']);
				array_push($arr, $obj);
			}
		} else {
			// echo '<h2> Search Result</h2>';
			// print($make);
		}
		mysqli_free_result($result);
		mysqli_close($conn);
	}

	exit(json_encode($arr));
?>